package com.yuneec.fly.utils;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.net.wifi.WifiManager.WifiLock;

import com.yuneec.fly.DroneApplication;

public class BindWifiManage {
    private WifiManager wifiManager;// 声明管理对象
    private WifiLock wifiLock;// WIFI 锁
    private List<ScanResult> scanResultList; // 扫描出来的网络连接列表
    private List<WifiConfiguration> wifiConfigList;// 网络配置列表
    private static BindWifiManage instance;


    private BindWifiManage() {
        wifiManager = (WifiManager) DroneApplication.getApplication().getApplicationContext().getSystemService(Context.WIFI_SERVICE);
    }
//
//    public BindWifiManage(WifiManager wifiManager)   {
//        this.wifiManager = wifiManager; // 获取 WIFI 服务
//        this.wifiInfo = wifiManager.getConnectionInfo(); // 得到连接信息
//    }

    public static BindWifiManage getBindWifiManage() {
        if (instance == null) {
            synchronized (BindWifiManage.class) {
                if (instance == null) {
                    instance = new BindWifiManage();
                }
            }
        }
        return instance;
    }

    public WifiManager getWifiManager() {
        return wifiManager;
    }

    // 获得 WIFI 状态
    public boolean getWifiStatus() {
        return wifiManager.isWifiEnabled();
    }

    // 打开 WIFI
    public boolean openWifi() {
        if (!wifiManager.isWifiEnabled()) {
            return wifiManager.setWifiEnabled(true);
        } else {
            return false;
        }
    }

    // 关闭 WIFI
    public boolean closeWifi() {
        if (!wifiManager.isWifiEnabled()) {
            return true;
        } else {
            return wifiManager.setWifiEnabled(false);
        }
    }

    public WifiInfo getWifiInfoObject() {
        return wifiManager.getConnectionInfo();
    }

    // 锁定 WIFI
    // 其实锁定 WIFI 就是判断 WIFI 是否建立成功，在这里使用的是held，握手的意思acquire 得到！
    public void lockWifi() {
        wifiLock.acquire();
    }

    // 解锁 WIFI
    public void unLockWifi() {
        if (!wifiLock.isHeld()) {
            wifiLock.release(); // 释放资源
        }
    }

    // 本来是写在构造函数中了，但是考虑到不是每次都会使用 WIFI 锁，
    // 所以干脆建立一个方法！需要时调用，建立就OK
    public void createWifiLock() {
        wifiLock = wifiManager.createWifiLock("fly"); // 创建一个锁的标志
    }

    // 指定配置好的网络进行连接
    public void connectConfiguration(int index) {
        // 索引大于配置好的网络索引返回
        if (index > wifiConfigList.size()) {
            return;
        }
        // 连接配置好的指定ID的网络
        wifiManager.enableNetwork(wifiConfigList.get(index).networkId,
                true);
    }

    // 扫描网络
    public void startScan() {
        wifiManager.startScan();
        scanResultList = wifiManager.getScanResults(); // 扫描返回结果列表
        wifiConfigList = wifiManager.getConfiguredNetworks(); // 扫描配置列表
    }

    public List<ScanResult> getWifiList() {
        return scanResultList;
    }

    public List<WifiConfiguration> getWifiConfigList() {
        return wifiConfigList;
    }

    // 获取扫描列表
    public StringBuilder lookUpscan() {
        StringBuilder scanBuilder = new StringBuilder();

        for (int i = 0; i < scanResultList.size(); i++) {
            scanBuilder.append("编号：" + (i + 1));
            scanBuilder.append(scanResultList.get(i).toString());  //所有信息
            scanBuilder.append("\n");
        }
        return scanBuilder;
    }

    //获取指定信号的强度
    public int getLevel(int NetId) {
        return scanResultList.get(NetId).level;
    }

    // 获取本机 MAC 地址
    public String getMac() {
        return (wifiManager.getConnectionInfo() == null) ? "" : wifiManager.getConnectionInfo().getMacAddress();
    }

    public String getConnectedBSSID() {
        return (wifiManager.getConnectionInfo() == null) ? null : wifiManager.getConnectionInfo().getBSSID();
    }

    public String getConnectedSSID() {
        return (wifiManager.getConnectionInfo() == null) ? null : wifiManager.getConnectionInfo().getSSID();
    }

    // 返回当前连接的网络的ID
    public int getCurrentNetId() {
        return (wifiManager.getConnectionInfo() == null) ? null : wifiManager.getConnectionInfo().getNetworkId();
    }

    // 返回所有信息
    public String getwifiInfo() {
        return (wifiManager.getConnectionInfo() == null) ? null : wifiManager.getConnectionInfo().toString();
    }

    // 获取IP地址
    public int getIP() {
        return (wifiManager.getConnectionInfo() == null) ? null : wifiManager.getConnectionInfo().getIpAddress();
    }

    // 添加一个连接
    public boolean addNetWordLink(WifiConfiguration config) {
        int NetId = wifiManager.addNetwork(config);
        return wifiManager.enableNetwork(NetId, true);
    }

    // 禁用一个链接
    public boolean disableNetWordLick(int NetId) {
        wifiManager.disableNetwork(NetId);
        return wifiManager.disconnect();
    }

    // 移除一个链接
    public boolean removeNetworkLink(int NetId) {
        return wifiManager.removeNetwork(NetId);
    }

    //不显示SSID
    public void hiddenSSID(int NetId) {
        wifiConfigList.get(NetId).hiddenSSID = true;
    }

    //显示SSID
    public void displaySSID(int NetId) {
        wifiConfigList.get(NetId).hiddenSSID = false;
    }

    /**
     *
     */

    // 获取扫描列表的SSID
    public ArrayList<String> getSSIDList() {
        ArrayList<String> mSSIDList = new ArrayList<String>();
        if (scanResultList != null) {
            for (ScanResult scanResult : scanResultList) {
                mSSIDList.add(scanResult.SSID);
            }
        }
        return mSSIDList;
    }
}
